#include <stdio.h>
#include "ofc/DRegEx.h"
#include "ofc/DText.h"

int main(int argc, char *argv[])
{
#ifdef HAVE_DREGEX 
  DRegEx *expr1 = [DRegEx alloc];
  DRegEx *expr2 = [DRegEx new  ];
  
  int     matched;
  int     index;

  [expr1 init :"a+b"];                 // Init with a regular expression

  matched = [expr1 match :"aaba"];     // Match a string with the regular expression
  if (matched == DRE_NO_MATCH)
    printf("String1 did not match the regular expression.\n");
  else
    printf("The first %d characters of string1 matched the regular expression.\n", matched);

  matched = [expr1 match :"baaba"];    // Match another string with the regular expression
  if (matched == DRE_NO_MATCH)
    printf("String2 did not match the regular expression.\n");
  else
    printf("The first %d characters of the string2 matched the regular expression.\n", matched);

  matched = [expr1 match :"baab" :1];  // Match a string with offset with the regular expression
  if (matched == DRE_NO_MATCH)
    printf("String3 did not match the regular expression.\n");
  else
    printf("The offseted %d characters of the string3 matched the regular expression.\n", matched);

  index = [expr1 search :"zyxab"];     // Search for the regular expression in the string
  if (index == DRE_NO_MATCH)
    printf("The regular expression could not be found in string4.\n");
  else
    printf("The regular expression started at %d in the string.\n", index);


  [expr2 icompile :"a(b+)a"];          // Compile a regular expression, case insensitive

  matched = [expr2 match :"abbba"];    // Match the string with the regular expression
  if (matched == DRE_NO_MATCH)
    printf("String5 did not match the regular expression.\n");
  else
  {
    DArray *matches = [expr2 matches :"abbba"]; // Get the matches from last match

    if (matches != nil)
    {
      for (index = 0; index < [matches length]; index++)
      {
        DText *str = [matches get :index];

        printf("Match %d:%s.\n", index, [str cstring]);
      }
      [matches free];
    }
  }

  [expr1 free];                        // Cleanup
  [expr2 free];
#else
  printf("DRegEx is not present in the library.\n");
#endif
  return 0;
}

